/**
 * 地图服务主模块
 * @author williamJM
 * date 2018/05/09 08:51
 */

'use strict';

require([
    "esri/Map",
    "esri/views/MapView",
    "sc/utils/utils",
    "esri/layers/GraphicsLayer",
    "sc/loadAirports/loadAirports",
    "sc/layerManage/layers",
    "sc/service/service",
    "dojo/domReady!"
], function(Map, MapView,utils,GraphicsLayer,loadAirports,layers,service) {

    var map = new Map({
        basemap: "oceans"
    });

    var view = new MapView({
        container: "map",
        map: map,
        center : [110,40],
        zoom : 3
    });

    utils.addHome(view);
    utils.addNavigationToggle(view);
    utils.addCompass(view);
    utils.addScaleBar(view);
    utils.addLocate(view);
    utils.addBasemapToggle(view);
    utils.addSearch(view);

    var airportLayer = layers.getAirportDynaLayer();
    var navDynaLayer = layers.getNavDynaLayer();
    var wayPointLayer = layers.getWayPointDynaLayer();
    var airWayLayer = layers.getAirwayDynaLayer();
    map.add(airportLayer);
    map.add(navDynaLayer);
    $(".airport").find("input[type=checkbox]").change(function () {
        var subLayerId = $(this).attr("data-layerId");
        var sublayer = airportLayer.findSublayerById(parseInt(subLayerId));
        sublayer.visible = !sublayer.visible;
    });

    $(".nav").find("input[type=checkbox]").change(function () {
        var subLayerId = $(this).attr("data-navLayerId");
        var sublayer = navDynaLayer.findSublayerById(parseInt(subLayerId));
        sublayer.visible = !sublayer.visible;
    });

    $(".way-point").on("click",function () {
        if(!$(this).hasClass("active")) {
            $(this).addClass("active");
            map.add(wayPointLayer);
            return;
        }
        $(this).removeClass("active");
        map.remove(wayPointLayer);
    });

    $(".air-way").on("click",function () {
        if(!$(this).hasClass("active")) {
            $(this).addClass("active");
            map.add(airWayLayer);
            return;
        }
        $(this).removeClass("active");
        map.remove(airWayLayer);
    });

    //初始化对外地图服务
    window.mapService = service.init(map,view);


});